﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Presentation.Pages
{
    public partial class CadastrarBoletim : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    carregarBoletins();
                    this.txtMes.Text = DateTime.Today.Month.ToString();
                }
            }
        }

        private void carregarBoletins()
        {
            try
            {
                using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                {
                    List<DEBoletim> boletins = banco.DEBoletims.OrderBy(q => q.Codigo).ToList();

                    grvBoletim.DataSource = boletins;
                    grvBoletim.DataBind();
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao carregar boletim");
            }
        }

        protected void btnExcluir_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    Button btnExcluir = sender as Button;
                    Int32 id = Convert.ToInt32(btnExcluir.CommandArgument);

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        DEBoletim boletim = banco.DEBoletims.Single(q => q.Codigo.Equals(id));

                        banco.DEBoletims.DeleteObject(boletim);
                        banco.SaveChanges();
                    }

                    carregarBoletins();

                    Util.showMessage(this, "Excluído com sucesso");
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao excluir boletim");
            }
        }

        private string getPostedFileName()
        {
            try
            {
                string fn = DateTime.Today.Year + this.txtMes.Text + ".pdf";
                return Server.MapPath("~/Boletins") + "\\" + fn;
            }
            catch (Exception)
            {
                throw;
            }
        }

        protected void btnAtivarDesativar_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Util.usuarioEhAdmin(this.Context))
                    Response.Redirect("~/Pages/index.aspx");
                else
                {
                    Button btn = sender as Button;
                    Int32 id = Convert.ToInt32(btn.CommandArgument);

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        DEBoletim boletim = banco.DEBoletims.Single(q => q.Codigo.Equals(id));
                        boletim.Ativo = !boletim.Ativo;

                        banco.SaveChanges();
                    }

                    carregarBoletins();

                    Util.showMessage(this, "Alterado com sucesso");
                }
            }
            catch (Exception)
            {
                Util.showMessage(this, "Erro ao alterar boletim");
            }
        }

        protected void btnSalvar_Click(object sender, EventArgs e)
        {
            try
            {
                if (Util.usuarioEhAdmin(this.Context))
                {
                    DEBoletim boletim = new DEBoletim();
                    boletim.Mes = Convert.ToInt32(txtMes.Text);
                    boletim.Ano = DateTime.Today.Year;
                    boletim.Ativo = this.chkAtivo.Checked;
                    boletim.Caminho = DateTime.Today.Year + this.txtMes.Text;

                    string SaveLocation = getPostedFileName();
                    fupBoletim.PostedFile.SaveAs(SaveLocation);

                    using (ministerioprosperid1Entities banco = new ministerioprosperid1Entities())
                    {
                        banco.AddToDEBoletims(boletim);
                        banco.SaveChanges();
                    }

                    this.txtMes.Text = DateTime.Today.Month.ToString();
                    chkAtivo.Checked = true;

                    carregarBoletins();

                    Util.showMessage(this, "Boletim cadastrado com sucesso");
                }
                else
                    Response.Redirect("~/Pages/index.aspx");
            }
            catch (Exception)
            {
                Util.showMessage(this, "Houve um erro ao cadastrar um novo Boletim");
            }
        }
    }
}